package com.yunfeng.common.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yunfeng.common.dao.ResourceDAO;
import com.yunfeng.common.entity.impl.Resource;
import com.yunfeng.common.service.ResourceService;
import com.yunfeng.common.util.Page;
import com.yunfeng.common.util.PageUtils;

@Service
@Transactional
public class ResourceServiceImpl implements ResourceService {

	@Autowired
	private ResourceDAO resourceDAO;

	/**
	 * @param resource
	 * @see com.yunfeng.common.service.ResourceService#save(com.yunfeng.common.entity.impl.Resource)
	 */
	@Override
	public void save(Resource resource) {
		resourceDAO.save(resource);
	}

	/**
	 * @param resource
	 * @see com.yunfeng.common.service.ResourceService#update(com.yunfeng.common.entity.impl.Resource)
	 */
	@Override
	public void update(Resource resource) {
		resourceDAO.save(resource);
	}

	/**
	 * @param id
	 * @see com.yunfeng.common.service.ResourceService#delete(java.lang.Long)
	 */
	@Override
	public void delete(Long id) {
		resourceDAO.delete(id);
	}

	/**
	 * @param id
	 * @return
	 * @see com.yunfeng.common.service.ResourceService#get(java.lang.Long)
	 */
	@Override
	public Resource get(Long id) {
		return resourceDAO.findOne(id);
	}

	/**
	 * @param uuid
	 * @return
	 * @see com.yunfeng.common.service.ResourceService#get(java.lang.String)
	 */
	@Override
	public Resource get(String uuid) {
		return resourceDAO.getByUuid(uuid);
	}

	/**
	 * @param page
	 * @return
	 */
	@Override
	public List<Resource> findAll(Page page) {
		org.springframework.data.domain.Page<Resource> springDataPage = resourceDAO
				.findAll(PageUtils.createPageable(page));
		page.setTotalCount(springDataPage.getTotalElements());
		return springDataPage.getContent();
	}

	/**
	 * 
	 * @param page
	 * @param name
	 * @return java.lang.String)
	 */
	@Override
	public List<Resource> findByName(Page page, String name) {
		org.springframework.data.domain.Page<Resource> springDataPage = resourceDAO
				.findByNameContaining(PageUtils.createPageable(page), name);
		page.setTotalCount(springDataPage.getTotalElements());
		return springDataPage.getContent();
	}

	/**
	 * 
	 * @param page
	 * @return
	 */
	@Override
	public List<Resource> find(Page page) {
		org.springframework.data.domain.Page<Resource> springDataPage = resourceDAO
				.findAll(PageUtils.createPageable(page));
		page.setTotalCount(springDataPage.getTotalElements());
		return springDataPage.getContent();
	}

}
